/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "otaSessionManager.h"

otaSessionManager::otaSessionManager()
{
	
}

otaSession& otaSessionManager::getSession( otaSessionKey sessionKey )
{
	std::map<otaSessionKey, otaSession>::iterator it = m_sessions.find(sessionKey);
	if(it != m_sessions.end())
	{
		return it->second;
	}
	else
	{
		std::cout << "Creating new session" << std::endl;
		if( sourceServer( sessionKey ) )
		{
			m_sessions[ sessionKey ].setInitData( sessionKey, sessionKey.getSourceIP(), sessionKey.getSourcePort(), sessionKey.getDestinationIP(), sessionKey.getDestinationPort() );
		}
		else
		{
			m_sessions[ sessionKey ].setInitData( sessionKey, sessionKey.getDestinationIP(), sessionKey.getDestinationPort(), sessionKey.getSourceIP(), sessionKey.getSourcePort() );
		}
		m_sessions[ sessionKey ].registerListener( this );
		return m_sessions[ sessionKey ];	
	}
}
	
bool otaSessionManager::init( otaConfigReader &config )
{
	std::string serverPortList;
	if( config.getConfiguration( "MySQL Server Ports", serverPortList ) )
	{
		m_serverPorts.clear();
		unsigned int offset = 0;
		std::string port = otaConfigReader::slice( serverPortList, ",", offset);
		while( port != "EOS" )
		{ 
			std::istringstream buffer(port);
			unsigned short nboPort;
			buffer >> nboPort;
			m_serverPorts.insert(htons(nboPort));
			port = otaConfigReader::slice( serverPortList, ",", offset);
		}
		return true;
	}
	else
	{
		return false;
	}
}	
	
void otaSessionManager::onSessionEnd( otaSessionKey &sessionKey )
{
	std::cout << "Session Ended" << std::endl;
	if( m_sessions[ sessionKey ].isValid() )
	{
		m_storage->storeSessionInformation( m_sessions[ sessionKey ] );
	}
	std::map< otaSessionKey, otaSession >::iterator it = m_sessions.find( sessionKey );
	m_sessions.erase( it );
}

void otaSessionManager::onSessionLogonFailed( otaSessionKey &sessionKey )
{
	std::cout << "Logon Failed" << std::endl;
	std::map< otaSessionKey, otaSession >::iterator it = m_sessions.find( sessionKey );
	m_sessions.erase( it );
}

void otaSessionManager::setStorageEngine( otaStorageEngine* storage )
{
	m_storage = storage;
}

bool otaSessionManager::sourceServer( otaSessionKey key )
{
	if(m_serverPorts.find( key.getSourcePort() ) != m_serverPorts.end())
		return true;
	else
		return false;
}


otaSessionManager::~otaSessionManager()
{
	
}


